function render() {
  // 获取数据库信息列表 并且渲染到页面
  $.ajax({
    type: 'GET',
    url: '/admin/swipers',
    success: function (res) {
      console.log(res)
      let str = ''
      let span = ''
      res.data.forEach(item => {
        if (item.swiperstatus === 1) {
          span = `<span data-id="${item.id}" data-status="2" class="layui-badge layui-bg-green">√</span>`
        } else {
          span = `<span data-id="${item.id}" data-status="1" class="layui-badge layui-bg-cyan">×</span>`
        }
        str += `
            <tr>
              <td>${item.id}</td>
              <td><img src="http://localhost:8888/uploads/${item.swiperimg}" alt="" /></td>
              <td>${item.swiperimg}</td>
              <td>${span}</td>
              <td>
                <button
                  type="button"
                  class="layui-btn layui-btn-danger layui-btn-sm del"
                  data-id="${item.id}"
                >
                  删除
                </button>
              </td>
            </tr>
      `
      });
      $('tbody').html(str)
    }
  })
}

render()

// 点击上传按钮 触发文件域按钮
$('#up').on('click', function () {
  $('#file').trigger('click')
})

$('#file').on('change', function (e) {
  let files = this.files
  // console.log(files)
  let fd = new FormData()
  // console.log(fd)
  for (let i = 0; i < files.length; i++) {
    fd.append('swipers', files[i])
  }
  // 提交用户选择的图片
  $.ajax({
    type: 'POST',
    url: '/admin/swipers',
    data: fd,
    processData: false,
    contentType: false,
    success: function (res) {
      // console.log(res)
      if (res.status === 0) {
        layer.msg(res.message)
        render()
      }
    }
  })
})

// 删除按钮
$('tbody').on('click', '.del', function () {
  let id = $(this).data('id')
  layer.confirm('确定要删除吗？', { icon: 3, title: '提示' }, function (index) {
    $.ajax({
      type: 'DELETE',
      url: `/admin/swipers/${id}`,
      success: function (res) {
        // console.log(res)
        if (res.status === 0) {
          layer.msg(res.message)
          render()
        }
      }
    })
    layer.close(index);
  });
})

// 状态小按钮
$('tbody').on('click', 'span', function () {
  let id = $(this).data('id')
  let status = $(this).data('status')
  // console.log(status)
  console.log(id)
  $.ajax({
    type: 'put',
    url: `/admin/swipers/${id}`,
    data: {
      status: status
    },
    success: function (res) {
      console.log(res)
      if (res.status === 0) {
        layer.msg(res.message)
        render()
      }
    }
  })
})